﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using MixScriptWeb.Share;
using MixScript.BLL;
using MixScriptWeb.control;
using MixScript.BLL.FI;

namespace MixScriptWeb.Accounting
{
    public partial class fChartAcc : cCommonWeb, IMIxCommon // System.Web.UI.Page
    {

        private static DataTable dt;
        private static SortDirection sd;
        private static cReferences oReff;
        private static cCOA oCoa;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {

                if (!Page.IsPostBack)
                {
                    addFindClickAttribute_COA(this, new MixScriptTB[] { txtParent }, new Label[] { lblParent });
                    //addConfirmation(this, new Button[] { btnDelete }, new string[] { "Are You sure want to delete this record?" });

                    //  setSortExpression(gvdetail, new string[] { "banknum", "bankname", "address", "city", "country", "ac" });

                }
                //lblParent.Text = ViewState["lblParent"];
                if (ViewState["lblParent"] != null)
                {
                    lblParent.Text = ViewState["lblParent"].ToString();
                }
                mn = (MainMenu)this.Master;
                hideStatusBar();

                //  fillgrid();
                oCoa = new cCOA();

                dt = new DataTable();
                //oCoa.getCoa(ref dt, 0, "", "", 0);
                //tvCOA.DataSource = dt;
                //tvCOA.DataBind();
                //PopulateTree();
                if (errMsg.Trim() != string.Empty)
                {
                    ShowStatusBar(errMsg);
                }
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        private void fillgrid()
        {
            try
            {
                //oReff = new cReferences(); //new cUserManagement();
                oCoa = new cCOA();
                dt = new DataTable();
                // TextBox tb = (TextBox)this.FindControl("ctl00$ContentPlaceHolder1$txtBankCode$txt");
                // Control[] ctl = new Control[] { txtBankCode, txtBankName, txtBankAddress, txtCity, txtCountry, txtAccBank };

                if (oCoa.getInquiry(ref dt, genInqParam(new Control[] { }), "accaccounts", out errMsg))
                {

                }
                else
                {
                    ShowStatusBar("Record Not Found", msgType.msgError);
                }
                bindGridview(gvdetail, dt, lbDelete);




            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message, msgType.msgError);
            }
        }


        #region Gridview
        #region Gridview Event

        protected void gvdetail_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {

                //oUM = new cUserManagement();
                //  btnNewState("Cancel");
                if (gvdetail.SelectedIndex > -1)
                {
                    txtAccID.Text = gvdetail.SelectedRow.Cells[1].Text;
                    txtAccName.Text = gvdetail.SelectedRow.Cells[2].Text;
                    txtParent.Text = gvdetail.SelectedRow.Cells[4].Text;
                    if (gvdetail.SelectedRow.Cells[3].Text == "D")
                    {
                        ddlAccType.SelectedIndex = 0;
                    }
                    else
                    {
                        ddlAccType.SelectedIndex = 1;
                    }
                    ddlProcess.SelectedItem.Value = gvdetail.SelectedRow.Cells[8].Text;
                    ddlApplication.SelectedItem.Value = gvdetail.SelectedRow.Cells[7].Text;
                    ddlSubApplication.SelectedItem.Value = gvdetail.SelectedRow.Cells[9].Text;
                }
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        protected void gvdetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (sd == SortDirection.Ascending)
            {
                sd = SortDirection.Descending;
            }
            else
            {
                sd = SortDirection.Ascending;
            }

            DataView dataView = new DataView(dt);
            dataView.Sort = e.SortExpression + " " + ShortDirection(sd);

            bindGridview(gvdetail, dataView, lbDelete);
        }

        protected void gvdetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.DataItemIndex == -1)
                    return;
                e.Row.Attributes.Add("onMouseOver", "this.style.cursor='pointer';");
                e.Row.Attributes.Add("onclick", this.GetPostBackClientEvent(gvdetail, "Select$" + e.Row.RowIndex.ToString()));
                //e.Row.Attributes.Add("onclick", this.GetPostBackEventReference(gvdetail, "Select$" + e.Row.RowIndex.ToString())); //  (gvdetail, "Select$" + e.Row.RowIndex.ToString()));


            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        protected void gvdetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            gvdetail.PageIndex = e.NewPageIndex;
            //gvdetail.DataBind();
            bindGridview(gvdetail, dt, lbDelete);

        }

        #endregion
        protected void cbAll_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                bool b = ((CheckBox)sender).Checked;


                foreach (GridViewRow gr in gvdetail.Rows)
                {
                    CheckBox cb = (CheckBox)gr.Cells[0].FindControl("cbitem");
                    cb.Checked = b;
                }

            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        protected void lbDelete_Click(object sender, EventArgs e)
        {
            try
            {
                oReff = new cReferences();
                bool isok = true;
                int i = 0;
                foreach (GridViewRow gr in gvdetail.Rows)
                {

                    if (((CheckBox)gr.Cells[0].FindControl("cbitem")).Checked)
                    {
                        string sid = gr.Cells[1].Text;

                        if (!oReff.deleteRowDB(new string[] { "BANKNUM" }, new string[] { sid }, "MST_BANK", out errMsg))
                        {
                            ShowStatusBar(em.getError(9) + sid + " " + errMsg);
                            isok = false;
                            continue;
                        }
                        else
                        {
                            i++;
                        }
                    }

                }

                if (i == 0 && isok)
                {
                    ShowStatusBar("Please Select Item First");
                }
                else if (isok)
                {
                    //    btnClear_Click(this, new EventArgs());
                    //  btnFind_Click(this, new EventArgs());
                    ShowStatusBar("Delete Completed");
                }
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        #endregion



        #region IMIxCommon Members

        public bool isvalid()
        {

            errMsg = "";
            //set semua control valid
            setValid(new MixScriptTB[] { txtAccID, txtAccName, txtParent });
            //cek semua field mandatory
            if (MustFilledOk(new Control[] { txtAccID, txtAccName }, out errMsg))
            {
                if (IntegerOnly(false, new Control[] { txtAccID }, out errMsg))
                {
                    cCOA oocoa = new cCOA();
                    if (oocoa.chekParent(txtParent.Text))
                    {
                        if (txtParent.Text.Trim() != string.Empty)
                        {
                            int l = txtParent.Text.Length;
                            if (txtAccID.Text.Trim().Substring(0, l) != txtParent.Text || txtParent.Text.Trim().Length == txtAccID.Text.Trim().Length)
                            {
                                ShowStatusBar("Invalid Account ID");
                                return false;
                            }


                        }
                        return true;
                    }
                    else
                    {
                        ShowStatusBar(em.getError(11));
                        return false;
                    }
                    //return true;

                }
                else
                {
                    ShowStatusBar(errMsg + " " + em.getError(10));
                    return false;
                }



            }
            else
            {
                ShowStatusBar(errMsg + " " + em.getError(2));
                return false;
            }


            //return true;

        }

        #endregion

        protected void btnNew_Click(object sender, EventArgs e)
        {

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (isvalid())
                {
                    Int64 idparent = 0;
                    Int64.TryParse(txtParent.Text.Trim(), out idparent);
                    Int64 idcoa = 0;
                    Int64.TryParse(txtAccID.Text.Trim(), out idcoa);
                    Int64 idProcess = 0;
                    Int64 idapplication = 0;
                    Int64 idsubapp = 0;
                    Int64.TryParse(ddlProcess.SelectedValue, out idProcess);
                    Int64.TryParse(ddlApplication.SelectedValue, out idapplication);
                    Int64.TryParse(ddlSubApplication.SelectedValue, out idsubapp);


                    oCoa = new cCOA();
                    int iret = 0;
                    if (oCoa.isRecordExist("tb_coamst", new string[] { "idcoa" }, new object[] { idcoa }))
                    {
                        if (oCoa.EditRowDtDB(out errMsg, "tb_coamst", new string[] { "idcoa" }, new object[] { idcoa }, false, new object[] { idcoa, txtAccName.Text, ddlAccType.Text, idparent, idProcess, idapplication, idsubapp }))
                        {
                            ShowStatusBar(em.getError(13));
                        }
                        else
                        {
                            ShowStatusBar("Edit Error");
                        }

                    }
                    else
                    {
                        iret = oCoa.addCoa(idcoa, txtAccName.Text, ddlAccType.Text, idparent, idProcess, idapplication, idsubapp);
                    }
                    if (iret == 0)
                    {
                        ShowStatusBar(em.getError(12));

                    }
                    else
                    {
                        ShowStatusBar(iret.ToString());
                    }

                    btnFind_Click(btnFind, new EventArgs());
                }
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                dt = new DataTable();
                oCoa = new cCOA();
                if (txtAccID.Text.Trim() != string.Empty)
                {
                    Int64 idcoa = 0;
                    Int64.TryParse(txtAccID.Text, out idcoa);
                    if (idcoa > 0)
                    {
                        if (oCoa.getCoa(ref dt, idcoa, txtAccName.Text, ddlAccType.Text, 0))
                        {
                            if (oCoa.haveChild(idcoa))
                            {
                                ShowStatusBar("This COA cannot be deleted. because any COA refer to this COA");
                            }
                            else
                            {
                                if (oCoa.deleteRowDB(new string[] { "idcoa" }, new object[] { idcoa }, "tb_coamst", out errMsg))
                                {
                                    ShowStatusBar("Delete Success");
                                }
                                else
                                {
                                    ShowStatusBar("Error while delete this coa");
                                }
                            }

                        }
                    }
                }
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }

        }

        protected void btnFind_Click(object sender, EventArgs e)
        {
            try
            {

                ViewState["lblParent"] = lblParent.Text;
                oCoa = new cCOA(); //new cUserManagement();
                dt = new DataTable();
                int idcoa = 0;
                if (txtAccID.Text.Trim() != string.Empty)
                {
                    // lblParent.Text + txtAccID.Text;
                    int.TryParse(lblParent.Text.Trim() + txtAccID.Text, out idcoa);
                }
                //TextBox tb = (TextBox)this.FindControl("ctl00$ContentPlaceHolder1$txtBankCode$txt");
                //Control[] ctl = new Control[] { txtBankCode, txtBankName, txtBankAddress, txtCity, txtCountry, txtAccBank };
                int iparent = 0;
                int.TryParse(txtParent.Text.Trim(), out iparent);
                //iparent = Convert.ToInt16(lblParent.Text);
                if (oCoa.getCoa(ref dt, idcoa, txtAccName.Text, ddlAccType.Text, iparent))
                {

                }
                else
                {
                    ShowStatusBar("Record Not Found", msgType.msgError);
                }
                bindGridview(gvdetail, dt, lbDelete);

            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message, msgType.msgError);
            }
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            try
            {
                //   ShowStatusBar(txtParent.Text);
                clearMixTB(new MixScriptTB[] { txtAccID, txtParent, txtAccName });
                ddlAccType.SelectedIndex = 0;
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }

        }
    }
}